package com.dreamlin.gankio.mvvm.girl

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import coil.api.load
import com.dreamlin.gankio.BR
import com.dreamlin.gankio.R
import com.dreamlin.gankio.base.ui.DiffBaseAdapter
import com.dreamlin.gankio.base.ui.BaseFragment
import com.dreamlin.gankio.base.ui.BaseViewHolder
import com.dreamlin.gankio.base.ui.ItemClickListener
import com.dreamlin.gankio.common.getScreenHeight
import com.dreamlin.gankio.common.isFastDoubleClick
import com.dreamlin.gankio.databinding.FragmentGirlBinding
import com.dreamlin.gankio.databinding.ItemGirlBinding
import com.dreamlin.gankio.mvvm.home.GankEntity
import com.dreamlin.gankio.mvvm.home.other.GankItemCallback
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.android.synthetic.main.fragment_girl.*
import kotlinx.android.synthetic.main.toolbar.*

/**
 * <p> Title: GirlFragment </p>
 * <p> Description: </p>
 * @author:  dreamlin
 * @date:    9/20/20
 * @version: V1.0.0
 * Created by dreamlin on 9/20/20.
 */
class GirlFragment : BaseFragment<FragmentGirlBinding, GirlViewModel>(),
    ItemClickListener<GankEntity> {
    private lateinit var adapter: DiffBaseAdapter<ItemGirlBinding, GankEntity, GankItemCallback>
    var maxPage: Int = 1
    var page: Int = 1
    val refreshDelay = 500

    override val layout: Int
        get() = R.layout.fragment_girl

    override fun initViews() {

        tvTitle.setText(R.string.navigation_girl)
        ivRight.visibility = View.VISIBLE
        rightClick.setOnClickListener(this)
        ivPhoto.setOnClickListener(this)
        tvSave.setOnClickListener(this)
        tvShare.setOnClickListener(this)
        rvGirls.setHasFixedSize(true)
        val gridLayoutManager = GridLayoutManager(requireContext(), 2)
        rvGirls.layoutManager = gridLayoutManager
        adapter = object :
            DiffBaseAdapter<ItemGirlBinding, GankEntity, GankItemCallback>(
                R.layout.item_girl, GankItemCallback()
            ) {
            override fun onBindViewHolder(holder: BaseViewHolder<ItemGirlBinding>, position: Int) {
                holder.binding.setVariable(BR.bean, viewModel.data.value?.get(position))
            }
        }
        adapter.setItemClickListener(this)
        rvGirls.adapter = adapter
        rvGirls.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            var scrollY = 0
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                scrollY += dy
                recyclerView.computeVerticalScrollOffset()
                if (!floating.isShown) {
                    if (scrollY >= getScreenHeight())
                        floating.show()

                } else {
                    if (scrollY < getScreenHeight())
                        floating.hide()
                }
            }
        })
        refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                if (page >= maxPage) {
                    toast(R.string.no_more_data)
                    refreshLayout.finishLoadMore(refreshDelay)
                    return
                }
//                viewModel.loadMoreData(++page, pageSize)
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                page = 1
//                viewModel.refreshData(adapter, entities)
            }
        })

        floating.setOnClickListener {
            if (isFastDoubleClick(it.id)) return@setOnClickListener
            rvGirls.smoothScrollToPosition(0)
        }
    }

    override fun initData() {
        super.initData()

        /* viewModel.girlEntities.observe(this, Observer {
             maxPage = it.page_count
             if (it.data.isNotEmpty()) {
                 when (refreshLayout.state) {
                     RefreshState.Refreshing -> {
                         entities.clear()
                         entities.addAll(it.data)
                         refreshLayout.finishRefresh()
                     }
                     RefreshState.Loading -> {
                         val start = entities.size
                         entities.addAll(it.data)
                         adapter.notifyItemRangeInserted(start, it.data.size)
                         refreshLayout.finishLoadMore()
                     }
                     RefreshState.None -> {
                         entities.addAll(it.data)
                         adapter.notifyDataSetChanged()
                     }
                 }
             } else {
                 if (refreshLayout.state == RefreshState.Refreshing) {
                     refreshLayout.finishRefresh()
                 } else if (refreshLayout.state == RefreshState.Loading) {
                     refreshLayout.finishLoadMore()
                 }
             }
         })
         viewModel.loadMoreData(1, pageSize)*/
    }

    override fun getVM(): Class<GirlViewModel> {
        return GirlViewModel::class.java
    }

    override fun click(v: View) {
        when (v.id) {
            R.id.rightClick -> {
                if (menu.visibility != View.VISIBLE) {
                    menu.visibility = View.VISIBLE
                }
            }
            R.id.tvSave -> {
                //TODO:save to storage

                menu.visibility = View.INVISIBLE
            }
            R.id.tvShare -> {
                // TODO: 2020/9/24 share image

                menu.visibility = View.INVISIBLE
            }
            R.id.ivPhoto
            -> ivPhoto.visibility = View.INVISIBLE
        }
    }

    override fun onItemClick(view: View, bean: GankEntity) {
        if (!isFastDoubleClick(view.id) && bean.images.isNotEmpty()) {
            ivPhoto.load(bean.images[0])
            ivPhoto.visibility = View.VISIBLE
        }
    }

}